#!/usr/bin/env node
"use strict";

const shell = require("shelljs");
const dateformat = require("dateformat");
const path = require("path");

module.exports = function oneReport(dirname) {
  // generate shell command and run
  const name = shell
    .exec("git config user.name", { silent: true })
    .stdout.trim();
  const daysAgo = new Date(new Date().getTime() - 1000 * 60 * 60 * 24);
  const dateStr = dateformat(daysAgo, "yyyy-mm-dd");
  const gitLogCommentToRun = `cd '${dirname}' && git log --after ${dateStr} --author '${name}'`;
  const gitLogs = shell
    .exec(gitLogCommentToRun, { silent: true })
    .stdout.trim();

  // reform gitLogs
  const logArr = gitLogs.split("\n");

  for (let i = logArr.length - 1; i >= 0; i--) {
    if (
      logArr[i].indexOf("commit") === 0 ||
      logArr[i].indexOf("Author") === 0 ||
      logArr[i].trim() === ""
    ) {
      logArr.splice(i, 1);
    }
  }

  for (let i = 0; i < logArr.length; i++) {
    if (logArr[i].indexOf("Date") === 0) {
      logArr.splice(i, 1);
    }
  }

  if (logArr.join().trim() !== "") {
    let folderRelativePath = path.relative(process.cwd(), dirname);
    if (folderRelativePath.trim() === "") {
      folderRelativePath = path.parse(__dirname).name;
    }
    console.log(`\n功能模块:${folderRelativePath}\n${logArr.join("\n")}`);
  }
};
